package com.liang.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.liang.mall.entity.Brand;
import com.liang.mall.group.Insert;
import com.liang.mall.util.ResultEntity;
import com.liang.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.rmi.server.ExportException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liang
 * @since 2021-05-18
 */
@Slf4j
@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    BrandService brandService;


    @ResponseBody
    @GetMapping("/list")
    public ResultEntity getBrandList(){

        List<Brand> brandList = brandService.list(new QueryWrapper<>());

        return ResultEntity.ok(brandList);


    }

    @ResponseBody
    @GetMapping("/getOne/{id}")
    public ResultEntity getOne(@PathVariable("id") int id){

        Brand byId = brandService.getById(id);

        return ResultEntity.ok(byId);

    }

    @ResponseBody
    @PostMapping("/update")
    public ResultEntity update(@RequestBody Brand brand){


        boolean update = brandService.update(brand, new QueryWrapper<Brand>().eq("brand_id", brand.getBrandId()));

        return update?ResultEntity.ok():ResultEntity.error();

    }

    @ResponseBody
    @RequestMapping("/add")

    public ResultEntity add(@RequestBody @Validated(Insert.class) Brand brand){


        boolean save = brandService.save(brand);

        return save?ResultEntity.ok():ResultEntity.error();

    }



    @RequestMapping("/download")
    public void download(HttpServletRequest req, HttpServletResponse resp,@RequestBody List<Integer> ids) throws UnsupportedEncodingException {

//        System.out.println(idList);
        //设置响应头
        resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("所有品牌信息.xlsx", "utf-8"));

        resp.setContentType("application/vnd.ms-excel");

//        Collection<Brand> brandList = brandService.listByIds(idList);
        Collection<Brand> brandList = brandService.list();

//        new ExportException("所有品牌数据","brandList");

        ExportParams exportParams = new ExportParams("所有的品牌数据", "brandList");

        Workbook workbook = ExcelExportUtil.exportBigExcel(exportParams, Brand.class, brandList);

        try {
            workbook.write(resp.getOutputStream());
            log.info("品牌信息 输出完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

